package com.michaelbulava.TFS.ui.wizards.share;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.tasks.vc.PerformShareTask;
import com.michaelbulava.TFS.ui.framework.wizard.ExtendedWizard;
import com.michaelbulava.TFS.ui.framework.wizard.ExtendedWizardStep;
import com.michaelbulava.TFS.ui.wizards.common.IConnectWizard;
import com.michaelbulava.TFS.ui.wizards.common.IShareWizard;
import com.michaelbulava.TFS.ui.wizards.common.ShareProjectOrModuleWizardStep;
import com.michaelbulava.TFS.ui.wizards.common.connect.SelectServerAndProjectsWizardStep;
import com.michaelbulava.TFS.ui.wizards.common.connect.SelectWorkspaceWizardStep;
import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.commonstructure.ProjectInfo;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import javafx.util.Pair;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * Created by mbulava on 6/23/2015.
 */
public class ShareWizard extends ExtendedWizard implements IShareWizard{


    //private final Module selectedModule;

    public ShareWizard(@Nullable Project project){
        this(project, null);
    }

    public ShareWizard(@Nullable Project project, @Nullable Module module) {
        super("Share Project", project);
        setShareEntireProject(module == null);
        setModulesToShare(new Module[] { module });
    }

    @Override
    protected List<? extends ExtendedWizardStep> getStepList() {
        List<ExtendedWizardStep> steps = new ArrayList<>();

        ShareProjectOrModuleWizardStep shareProjectOrModuleWizardStep = new ShareProjectOrModuleWizardStep(this);
        steps.add(shareProjectOrModuleWizardStep);
        steps.add(new SelectServerAndProjectsWizardStep(this, false));
        steps.add(new SelectWorkspaceWizardStep(this));
        steps.add(new MapProjectToTFSWizardStep(this));
        steps.add(new MapModulesToTFSWizardStep(this));
        steps.add(new ShareModulesSummaryWizardStep(this));
        steps.add(new ShareProjectSummaryWizardStep(this));

        return steps;
    }

    @Override
    public Object getNextStepId(ExtendedWizardStep currentStep) {
        if (currentStep instanceof ShareProjectOrModuleWizardStep)
            return SelectServerAndProjectsWizardStep.STEP_ID;
        else if (currentStep instanceof SelectServerAndProjectsWizardStep)
            return SelectWorkspaceWizardStep.STEP_ID;
        else if (currentStep instanceof SelectWorkspaceWizardStep)
            if (getShareEntireProject())
                return MapProjectToTFSWizardStep.STEP_ID;
            else
                return MapModulesToTFSWizardStep.STEP_ID;
        else if (currentStep instanceof MapProjectToTFSWizardStep)
            return ShareProjectSummaryWizardStep.STEP_ID;
        else if (currentStep instanceof MapModulesToTFSWizardStep)
            return ShareModulesSummaryWizardStep.STEP_ID;
        return null;
    }

    @Override
    public Object getPreviousStepId(ExtendedWizardStep currentStep) {
        if (currentStep instanceof ShareProjectOrModuleWizardStep)
            return null;
        else if (currentStep instanceof SelectServerAndProjectsWizardStep)
            return ShareProjectOrModuleWizardStep.STEP_ID;
        else if (currentStep instanceof SelectWorkspaceWizardStep)
            return SelectServerAndProjectsWizardStep.STEP_ID;
        else if (currentStep instanceof MapModulesToTFSWizardStep ||
                currentStep instanceof MapProjectToTFSWizardStep)
            return SelectWorkspaceWizardStep.STEP_ID;
        else if (currentStep instanceof ShareProjectSummaryWizardStep)
            return MapProjectToTFSWizardStep.STEP_ID;
        else if (currentStep instanceof ShareModulesSummaryWizardStep)
            return MapProjectToTFSWizardStep.STEP_ID;
        return null;
    }

//    @Override
//    public Module getCurrentModule() {
//        return selectedModule;
//    }


    @Override
    public boolean getShareEntireProject() {
        return ((Boolean)getWizardData(BOOL_ENTIRE_PROJECT)) == Boolean.TRUE;
    }

    @Override
    public void setShareEntireProject(boolean value) {
        setWizardData(BOOL_ENTIRE_PROJECT, value);
    }

    @Override
    public Module[] getModulesToShare() {
        Object value = getWizardData(MODULES_ARRAY);
        if (value != null && value instanceof Module[])
            return (Module[])value;
        return null;
    }

    @Override
    public void setModulesToShare(Module[] selected) {
        setWizardData(MODULES_ARRAY, selected);
    }

    @Override
    public TFSTeamProjectCollection getTeamProjectCollection() {
        Object value = getWizardData(IConnectWizard.TFSTEAMPROJECTCOLLECTION);
        if (value != null && value instanceof  TFSTeamProjectCollection)
            return (TFSTeamProjectCollection)value;
        return null;
    }

    @Override
    public void setTeamProjectCollection(TFSTeamProjectCollection collection) {
        setWizardData(IConnectWizard.TFSTEAMPROJECTCOLLECTION, collection);
    }

    @Override
    public ProjectInfo[] getTeamProjects() {
        Object value = getWizardData(IConnectWizard.PROJECTINFO_ARRAY);
        if (value != null && (value instanceof ProjectInfo[]))
            return (ProjectInfo[])value;
        return null;
    }

    @Override
    public void setTeamProjects(ProjectInfo[] selected) {
        setWizardData(IConnectWizard.PROJECTINFO_ARRAY, selected);
    }

    @Override
    public Workspace getWorkspace() {
        Object value = getWizardData(IConnectWizard.WORKSPACE);
        if (value != null && value instanceof  Workspace)
            return (Workspace)value;
        return null;
    }

    @Override
    public void setWorkspace(Workspace workspace) {
        setWizardData(IConnectWizard.WORKSPACE, workspace);
    }

    @Override
    public String getProjectShareLocation() {
        Object value = getWizardData(PROJECT_TFS_LOCATION);
        if (value != null && value instanceof String)
            return (String)value;
        return null;
    }

    @Override
    public void setProjectShareLocation(String value) {
        if (!getShareEntireProject())
            throw new IllegalArgumentException("ProjectShareLocation cannot be set when entire project is not being shared");
        setWizardData(PROJECT_TFS_LOCATION, value);
    }

//    @Override
    private Map<Module, String> getModuleShareLocations() {
        Object value = getWizardData(MODULE_TFS_LOCATION_MAPPING);
        Map<Module, String> map = null;
        if (value == null || !(value instanceof Dictionary))
            map = new HashMap<Module, String>();
        else
            map = (HashMap<Module,String>)value;
        return map;
    }

    @Override
    public void setModuleShareLocation(Module forModule, String serverLocation) {
        Map<Module, String> map = getModuleShareLocations();
        map.put(forModule, serverLocation);
        setWizardData(MODULE_TFS_LOCATION_MAPPING, map);
    }

    @Override
    public String getModuleShareLocation(Module module) {
        return getModuleShareLocations().get(module);
    }

    /**
     *
     */
    @Override
    public void performShare(){
        if (getExitCode() != ShareWizard.OK_EXIT_CODE){
            Messages.showErrorDialog(getProject(), "Wizard ended not Ok.", "Cannot Share");
            return;
        }
        PerformShareTask task;
        if (getShareEntireProject())
            task = new PerformShareTask(getProject(), getTeamProjects(), getWorkspace(), getProjectShareLocation(), false);
        else
            task = new PerformShareTask(getProject(), getTeamProjects(), getWorkspace(), getModuleShareLocations(), false);
        ProgressManager.getInstance().run(task);
    }
}
